{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% import "_macros.html" as macros %}

{% block title %}语音识别模型基准测试系统{% endblock %}

{% block page_content %}
<div class="page-header">
    <a href="{{ url_for('.bt_service') }}">返回基准测试系统接口总页</a>
    <p>ip: {{ bt_host.ip }}</p>
    <p>端口: {{ bt_host.port }}</p>
    <p>上次活动时间: {{ bt_host.last_active }}</p>
    {% if bt_host.b_connect %}
    <p>连通性: 可连通</p>
    <a class="btn btn-default" href="javascript:void(0)" onclick="start_detect_devices({{ bt_host.id }})">检测计算设备</a>
    <div class="row">
        <table class="table">
            <thead>
                <tr>
                    <td>名称</td>
                    <td>设备号</td>
                    <td>最高频率</td>
                    <td>最大内存量</td>
                    <td>描述</td>
                    <td>操作</td>
                </tr>
            </thead>
            <tbody id="bt_devices">
                {% if bt_devices|length>0 %}
                {% for bt_device in bt_devices %}
                <tr>
                    <td>{{ bt_device.name }}</td>
                    <td>{{ bt_device.device_id }}</td>
                    <td>{{ bt_device.max_frequency }}</td>
                    <td>{{ bt_device.memory_size }}</td>
                    <td>{% if bt_device.description %}{{ bt_device.description }}{% endif %}</td>
                    <td><a class="btn btn-default" href="{{ url_for('.bt_device_description',id=bt_device.id) }}">设置备注</a></td>
                </tr>
                {% endfor %}
                {% else %}
                <tr>
                    <td colspan="6">还未检测该接口所在主机上的计算设备</td>
                </tr>
                {% endif %}
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="6">
                        <span id="span-1"></span><br>
                        <span id="span-2"></span><br>
                        <span id="span-3"></span>
                    </td>
                </tr>
            </tfoot>
        </table>
    </div>
    {% else %}
    <p>连通性: 不可连通，执行基准测试任务接口可连接后方能检测目标主机上的计算设备</p>
    {% endif %}
</div>
<script type="text/javascript">
    function start_detect_devices(bt_host_id){
        $.ajax({
            type:'POST',
            url:'/detect_devices/'+bt_host_id,
            success:function(data,status,request){
                status_url=request.getResponseHeader('Location');
                update_status(status_url);
            },
            error:function(){
                alert('请求检测主机上的计算设备的过程中出现错误');
            }
        });
    }
    function update_status(status_url){
        $.getJSON(status_url,function(data){
            $('#span-1').text($('#span-2').text());
            $('#span-2').text($('#span-3').text());
            if ('result' in data){
                $('#span-3').text(data['time']+' '+data['status']);
                let result=$.parseJSON(data['result']);
                console.log(result);
                let devices=result['devices'];
                console.log(devices);
                tbody_html=''
                $('#bt_devices').empty();
                for(let i=0;i<devices.length;i++){
                    tbody_html+='<tr><td>'+devices[i]['name']+'</td><td>'+devices[i]['device_id']+'</td><td>'+devices[i]['freq']+'</td><td>'+devices[i]['mem_size']+'</td><td></td><td><a class="btn btn-default" href="/bt_device_description/'+devices[i]['id']+'">设置备注</a></td></tr>';
                }
                $('#bt_devices').append(tbody_html);
                // console.log(data['result']);
                // window.location.reload();
            }else{
                $('#span-3').text(data['time']+' '+data['state']+' '+data['status']);
            }
            if(data['state']=='PENDING'||data['state']=='PROGRESS'){
                setTimeout(function(){
                    update_status(status_url);
                },1000);
            }
        })
    }
</script>
{% endblock %}